﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using System.Globalization;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Data.SqlClient;


public partial class sales : UserControl
{

    float summarise = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            clrDate.SelectedDate = DateTime.Today;
    }

    protected void getTills(object sender, EventArgs e)
    {
        using (OleDbConnection cn = new OleDbConnection(ConfigurationManager.ConnectionStrings["DB04Connect"].ToString().Replace("DB_Ex", "DBMySQLTill")))
        {
            OleDbCommand myCMD = new OleDbCommand("Till_MYSQL_get_tills_data", cn);
            myCMD.CommandType = CommandType.StoredProcedure;

            OleDbParameter date = myCMD.Parameters.Add("@dateStart", OleDbType.VarChar, 20);
            date.Direction = ParameterDirection.Input;

            OleDbParameter server = myCMD.Parameters.Add("@server", OleDbType.VarChar, 100);
            server.Direction = ParameterDirection.Input;


            SPWeb web = SPControl.GetContextWeb(this.Context);
            UserProfileManager profileManager = new UserProfileManager();

            UserProfile user = profileManager.GetUserProfile(web.CurrentUser.LoginName);

            date.Value = clrDate.SelectedDate.ToString("yyyy-MM-dd");
            server.Value = "DBMYSQLTILL_ALLSHOPS";
            cn.Open();

            //OleDbDataReader myReader = myCMD.ExecuteReader();

            OleDbDataAdapter adapter = new OleDbDataAdapter(myCMD);
            DataSet dataSet1 = new DataSet();
            dataSet1.Tables.Clear();
            adapter.Fill(dataSet1, "Table");
            //GridView1.AllowPaging = true;
            //GridView1.PageSize = 30;			
            //GridView1.AllowSorting = true;			
            GridView1.DataSource = dataSet1.Tables["Table"];

            GridView1.DataBind();

            decimal result = 0;
            //Label1.Text = "";//"<strong>"+user["office"].ToString()+": </strong> ";

            //while (myReader.Read())
            //{
            //Label1.Text += myReader.GetValue(0).ToString()+"<br/>";
            //Label1.Text += string.Format("<b>Касса {1}:</b> {0}  &nbsp;&nbsp;", System.Math.Round(decimal.Parse(myReader.GetValue(0).ToString()),2, System.MidpointRounding.AwayFromZero), System.Math.Round(decimal.Parse(myReader.GetValue(1).ToString())),2, System.MidpointRounding.AwayFromZero);
            //result = result + System.Math.Round(decimal.Parse(myReader.GetValue(0).ToString()),2, System.MidpointRounding.AwayFromZero);
            //}

            //Label1.Text += "<b>Итого:</b> " + result +"<br/>";
            //myReader.Close();
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            summarise += float.Parse(e.Row.Cells[5].Text);
            e.Row.Cells[5].Text = ReturnNumericStringFormat(float.Parse(e.Row.Cells[5].Text));
            e.Row.Cells[4].Text = ReturnNumericStringFormat(float.Parse(e.Row.Cells[4].Text));
            e.Row.Cells[3].Text = ReturnNumericStringFormat(float.Parse(e.Row.Cells[3].Text));
            e.Row.Cells[2].Text = ReturnNumericStringFormat(float.Parse(e.Row.Cells[2].Text));
            e.Row.Cells[1].Text = ReturnNumericStringFormat(float.Parse(e.Row.Cells[1].Text));
        }
        if (e.Row.RowIndex == -1)
        {
            Label1.Font.Bold = true;
            Label1.ForeColor = System.Drawing.Color.Green;
            string lesum = String.Empty;
            try
            {
                lesum = ReturnNumericStringFormat(summarise).ToString() + " рублей.";
            }
            catch (Exception eee) { Label1.Text = eee.ToString(); }
            Label1.Text = "Итого: " + lesum;
        }

    }

    protected void clrDate_SelectionChanged(Object sender, EventArgs e)
    {
        //Label1.Text = clrDate.SelectedDate.ToString("yyyy-MM-dd");
        getTills(null, null);
    }

    string ReturnNumericStringFormat(float mFloat)
    {
        //string tTemp1 = mFloat.ToString();
        string tTemp1 = String.Format("{0:0.00}", mFloat);
        int tLen = tTemp1.Length;
        int tCount = (tLen - 3) / 3;
        if ((tLen - 3 - 3 * tCount) == 0) tCount--;
        int tStart = tLen - 3 - 3 * tCount;
        string tRet = tTemp1.Substring(0, tStart);
        for (int i = 0; i < tCount; i++)
        {
            tRet += " ";
            tRet += tTemp1.Substring(tStart + i * 3, 3);
        }
        //tRet += tTemp1.Substring(tStart + tCount * 3, 3);
        return tRet;
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {

        }
        //Lerror.Text += e.Row.Cells.Count;
        e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
        e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
        e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
        e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
        e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Right;
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        using (OleDbConnection cn = new OleDbConnection(ConfigurationManager.ConnectionStrings["DB04Connect"].ToString().Replace("DB_Ex", "DBMySQLTill")))
        {
            OleDbCommand myCMD = new OleDbCommand("Till_MYSQL_get_tills_data", cn);
            myCMD.CommandType = CommandType.StoredProcedure;

            OleDbParameter date = myCMD.Parameters.Add("@dateStart", OleDbType.VarChar, 20);
            date.Direction = ParameterDirection.Input;

            OleDbParameter server = myCMD.Parameters.Add("@server", OleDbType.VarChar, 100);
            server.Direction = ParameterDirection.Input;


            SPWeb web = SPControl.GetContextWeb(this.Context);
            UserProfileManager profileManager = new UserProfileManager();

            UserProfile user = profileManager.GetUserProfile(web.CurrentUser.LoginName);

            date.Value = clrDate.SelectedDate.ToString("yyyy-MM-dd");
            server.Value = "DBMYSQLTILL_ALLSHOPS";
            cn.Open();

            OleDbDataAdapter adapter = new OleDbDataAdapter(myCMD);
            DataSet dataSet1 = new DataSet();
            dataSet1.Tables.Clear();
            adapter.Fill(dataSet1, "Table");
            //Lerror.Text= e.SortDirection.ToString();
            if (dataSet1.Tables["Table"] != null)
            {
                DataView dvSortedView = new DataView(dataSet1.Tables["Table"]);

                dvSortedView.Sort = e.SortExpression + " " + "DESC";


                //Lerror.Text= ViewState["sorting"].ToString();

                GridView1.DataSource = dvSortedView;
                GridView1.DataBind();
            }
        }
    }
}
